To adjoin we will focus on matching with buffer of the reserve location and use the tax plot match to adjoin to the property data. Add a buffer distance of 10 meters and then match with the Hawaii tmz file.
Code
reserves <-st_transform(reserves, 32605)reserves_buffered <-st_buffer(reserves, dist =10)reserves_buffered <-st_transform(reserves_buffered, 4326)intersections <-st_intersects(HI_2023_shapefile, reserves_buffered)HI_intersections <- HI_2023_shapefile[lengths(intersections) >0, ]ggplot() +geom_sf(data = HI_intersections, fill ="brown", color ="grey") +geom_sf(data = reserves_buffered, fill ="forestgreen", color ="black") +geom_sf(data = coastline, color ="black") +coord_sf(xlim =c(-158.5, -157.5), # longitudesylim =c(21, 21.9), # latitudes in correct orderexpand =FALSE ) +theme_minimal() +labs(title ="Buffered Reserves (Green) and Adjoining Properties (Brown)")
Following code to match each adjoin reserve to property. Property can adjoin multiple reserves names so we combine them by comma in one column.
Code
# Create a vector of indices for the first overlapping shp2 feature per shp1 rowHI_2023_shapefile$reserve_names <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$name[idx], collapse =", ") }})HI_2023_shapefile$managedby <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$managedby[idx], collapse =", ") }})HI_2023_shapefile$type_defin <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$type_defin[idx], collapse =", ") }})
Source Code
---format: html: code-fold: true # Enables dropdown for code code-tools: true # (Optional) Adds buttons like "Show Code" code-summary: "Show code" # (Optional) Custom label for dropdown toc: true toc-location: left page-layout: fulleditor: visual---# AmenitiesThese are amenities which currently include reserves and gold course. Trails and Beaches have their own page.# Golf Courses```{r cache=TRUE, echo=TRUE, warning=FALSE, message=FALSE}source("_common.R")golf=read_sf("/Users/ashleylowe/Trails/golf_courses.shp/golf_courses.shp")golf=st_transform(golf, 4326 )golf=golf[c(1,7,8,12)]tmap_mode("view")center_lon <--157.45center_lat <-20.75zoom_level <-7tm_shape(golf) +tm_dots(col ="Name",palette ="magma",size =0.1,id ="Name",popup.vars =c("Name"="Name"),legend.show =FALSE# hides the legend ) +tm_basemap() +tm_view(set.view =c(center_lon, center_lat, zoom_level))```## Distance Between PointsDistance to each Property calculation```{r eval=FALSE}# 1. find nearest feature indexnearest_idx <-st_nearest_feature(PB_HawaiiInfo, golf)# 2. get nearest trails (same row order as PB_HawaiiInfo)nearest_trails <- golf[nearest_idx, ]# 3. calculate distance (element-wise)nearest_dist_m <-st_distance(PB_HawaiiInfo, nearest_trails, by_element =TRUE)# 4. merge trail attributes + distance into PB_HawaiiInfoPB_HawaiiInfo <- PB_HawaiiInfo %>%mutate(nearest_golf =as.numeric(nearest_dist_m) ) %>%bind_cols(st_drop_geometry(nearest_trails)) # adds trail attributes```# Nature ReservesCentralized location of managed lands protecting natural and cultural resources of the Main Hawaiian Islands and Kure Atoll.We will use this one to create a buffer and identify the properties that adjoin a reserve.```{r cache=TRUE, echo=TRUE, warning=FALSE, message=FALSE}reserves <-read_sf("/Users/ashleylowe/Trails/Reserves/Reserves.shp")reserves$managedby <-gsub("[()]", "", reserves$managedby)tmap_mode("view")center_lon <--157.45center_lat <-20.75zoom_level <-7tm_shape(reserves) +tm_polygons("managedby",palette ="magma",id ="name",popup.vars =c("Managed By"="managedby"),legend.show =FALSE# hides the legend ) +tm_basemap() +tm_view(set.view =c(center_lon, center_lat, zoom_level))```### Matching polygonsTo adjoin we will focus on matching with buffer of the reserve location and use the tax plot match to adjoin to the property data. Add a buffer distance of 10 meters and then match with the Hawaii tmz file.```{r cache=TRUE, echo=TRUE, warning=FALSE, message=FALSE}reserves <-st_transform(reserves, 32605)reserves_buffered <-st_buffer(reserves, dist =10)reserves_buffered <-st_transform(reserves_buffered, 4326)intersections <-st_intersects(HI_2023_shapefile, reserves_buffered)HI_intersections <- HI_2023_shapefile[lengths(intersections) >0, ]ggplot() +geom_sf(data = HI_intersections, fill ="brown", color ="grey") +geom_sf(data = reserves_buffered, fill ="forestgreen", color ="black") +geom_sf(data = coastline, color ="black") +coord_sf(xlim =c(-158.5, -157.5), # longitudesylim =c(21, 21.9), # latitudes in correct orderexpand =FALSE ) +theme_minimal() +labs(title ="Buffered Reserves (Green) and Adjoining Properties (Brown)")```Following code to match each adjoin reserve to property. Property can adjoin multiple reserves names so we combine them by comma in one column. ```{r eval=FALSE}# Create a vector of indices for the first overlapping shp2 feature per shp1 rowHI_2023_shapefile$reserve_names <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$name[idx], collapse =", ") }})HI_2023_shapefile$managedby <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$managedby[idx], collapse =", ") }})HI_2023_shapefile$type_defin <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$type_defin[idx], collapse =", ") }})```